#pragma once
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace Net_Work
{
#define CONV(addrptr) (struct sockaddr *)(addrptr)
    enum
    {
        SockError = 0,
        BindError,
        ListenError,
        AcceptError,
    };

    static const int defualtsockfd = -1;
    const int defaultbacklog = 5;
    class Socket
    {
    public:
        virtual ~Socket() {}
        virtual void CreateSocketOrDie() = 0;
        virtual void BindSocketOrDie(uint16_t port) = 0;
        virtual void ListenSocketOrDie(int backlog) = 0;
        virtual Socket *AcceptConnection(std::string *peerip, uint16_t *peerport) = 0; // 服务器监听
        virtual bool ConnectServer(std::string &serverip, uint16_t serverport) = 0;
        virtual int GetSockfd() = 0;
        virtual bool SetSockfd(int sockfd) = 0;
        virtual void CloseSockfd() = 0;
        virtual bool Recv(std::string *buffer,int size) =0;
        virtual void Send(std::string& send_message) =0;
        
    public:
        void BuildListenSocketMethod(uint16_t port, int backlog)
        {
            CreateSocketOrDie();
            BindSocketOrDie(port);
            ListenSocketOrDie(backlog);
        }

        bool BuildConnectSocketMethod(std::string& serverip, uint16_t serverport)
        {
            CreateSocketOrDie();
            return ConnectServer(serverip, serverport);
        }

        void BuildNormalSocketMethod(int sockfd)
        {
            SetSockfd(sockfd);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int sockfd = defualtsockfd) : _sockfd(sockfd) {}
        ~TcpSocket() {}

    public:
        void CreateSocketOrDie() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
                exit(SockError);
        }

        void BindSocketOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;

            // 绑定本地
            int n = ::bind(_sockfd, CONV(&local), sizeof(local));
            if (n < 0)
                exit(BindError);
        }

        void ListenSocketOrDie(int backlog = defaultbacklog) override
        {
            int n = listen(_sockfd, backlog);
            if (n < 0)
                exit(defaultbacklog);
        }

        Socket *AcceptConnection(std::string *peerip, uint16_t *peerport) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(len);
            int newfd = accept(_sockfd, CONV(&peer), &len);
            if (newfd < 0)
                exit(AcceptError);
            *peerip = inet_ntoa(peer.sin_addr);
            *peerport = ntohs(peer.sin_port);

            Socket *s = new TcpSocket(newfd);
            return s;
        }

        bool ConnectServer(std::string& serverip, uint16_t serverport) override
        {
            struct sockaddr_in server;
            bzero(&server, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(serverport);
            //inet_pton(AF_INET, serverip.c_str(), &server);
            server.sin_addr.s_addr = inet_addr(serverip.c_str());
            int n = ::connect(_sockfd, CONV(&server), sizeof(server));

            if (n == 0)
                return true;
            else
                return false;
        }

        int GetSockfd() override
        {
            return _sockfd;
        }

        bool SetSockfd(int sockfd) override
        {
            _sockfd = sockfd;
        }

        void CloseSockfd() override
        {
            if (_sockfd > defualtsockfd)
                close(_sockfd);
        }

        bool Recv(std::string *inbuffer,int size) override
        {
            char buffer[size];
            ssize_t n=recv(_sockfd,buffer,sizeof(buffer),0);
            if(n>0)
            {
                buffer[n]=0;
                *inbuffer+=buffer;
                return true;
            }
            else if(n==0)
                return false;
            else 
                return true;
        }

        void Send(std::string& send_message) override
        {
            send(_sockfd,send_message.c_str(),send_message.size(),0);
        }
    private:
        int _sockfd;
    };
}
